package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os/user"
	"wmi_info/utils"
)

//获取信息结构体
type WindowsOS struct {
	Processors      []utils.Win32_Processor           `json:"processors"`
	Memory          []utils.Win32_PhysicalMemory      `json:"memory"`
	NetworkAdapters []utils.Win32_NetworkAdapter      `json:"network_adapters"`
	Computer        utils.Win32_ComputerSystemProduct `json:"computer"`
	Host            utils.Win32_ComputerSystem        `json:"host"`
	Loginuser       string                            `json:"loginuser"`
}

func windows() WindowsOS {
	var localhost WindowsOS
	processors := utils.Processor()
	//for i, item := range processors {
	//	fmt.Println("CPU" + strconv.Itoa(i) + "：" + item.Name)
	//}
	localhost.Processors = processors

	memory := utils.Memory()
	//for _, item := range memory {
	//	fmt.Println("Bank："+item.BankLabel, "Manufacturer："+item.Manufacturer, "Type："+item.PartNumber, "Memory："+strconv.Itoa(item.Capacity/1024/1024/1024)+"G")
	//}
	localhost.Memory = memory

	networkAdapters := utils.NetworkAdapter()
	//for _, item := range networkAdapters {
	//	fmt.Println("Adapter："+item.Description, "MAC："+item.MACAddress)
	//}
	localhost.NetworkAdapters = networkAdapters

	computer := utils.Computer()
	//fmt.Println("Series："+computer.Name, "Manufacturer："+computer.Vendor, "Number："+computer.IdentifyingNumber, "Type："+computer.Version)
	localhost.Computer = computer

	host := utils.Host()
	//fmt.Println("HostName："+host.DNSHostName, "Domain："+host.Domain)
	localhost.Host = host

	loginuser, _ := user.Current()
	localhost.Loginuser = loginuser.Username

	return localhost
}

func main() {
	info := windows()
	
	all, _ := json.Marshal(&info)
	fmt.Println(string(all))
	//写入本地文件
	_ = ioutil.WriteFile(info.Host.DNSHostName+"_.json", all, 0644)
}
